package nl.hva.web.mvcwebapp.controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import nl.hva.web.mvcwebapp.doa.CourseDAO;
import nl.hva.web.mvcwebapp.doa.NewsDAO;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.models.News;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class NewsController extends HttpServlet {

    private String address = "news.jsp";
    ArrayList paths_jsp = new ArrayList();
    ArrayList paths = new ArrayList();
    
    /*
     * HTTP GET request
     */
    
    public ArrayList getPaths() {
        ArrayList paths = new ArrayList();
        paths.add(0, "/news");
        paths.add(1, "/news/new");
        paths.add(2, "/news/edit");
        paths.add(3, "/news/delete");
        paths.add(4, "/news/view");
        paths.add(5, "/news/view/course");
        return paths;
    }

    public ArrayList getPathsJSP() {
        ArrayList paths_jsp = new ArrayList();
        paths_jsp.add(0, "/news/news.jsp");
        paths_jsp.add(1, "/news/news_edit.jsp");
        paths_jsp.add(2, "/news/news_edit.jsp");
        paths_jsp.add(3, "/news/news.jsp");
        paths_jsp.add(4, "/news/news_view.jsp");
        paths_jsp.add(5, "/news/news_view_course.jsp");
        return paths_jsp;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        boolean redirect = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        
        
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    // /news
                    if (i == 0) {
                        List<News> nieuwslijst = session.createCriteria(News.class).list();
                        tx.commit();
                        
                        request.setAttribute("pageTitle", "News");
                        request.setAttribute("nieuwsUitSessie", nieuwslijst);
                        request.setAttribute("aantalNieuws", nieuwslijst.size());
                    } // new news item
                    else if (i == 1) {
                        List<Course> courselist = CourseDAO.getAllCourses();
                        request.setAttribute("courses", courselist);
                        request.setAttribute("amountcourses", courselist.size());
                        request.setAttribute("pageTitle", "New Newsitem");
                        request.setAttribute("buttonTitle", "Add");
                    }// edit news item
                    else if (i == 2) {
                        int newsid = Integer.parseInt(request.getParameter("newsid"));
                        News news = NewsDAO.getNews(newsid);
                        List<Course> courselist = CourseDAO.getAllCourses();
                        request.setAttribute("courses", courselist);
                        request.setAttribute("amountcourses", courselist.size());
                        request.setAttribute("news", news);
                        request.setAttribute("newsid", newsid);
                        request.setAttribute("pageTitle", "Edit news");
                    }//delete news item
                    else if (i == 3) {
                        int newsid = Integer.parseInt(request.getParameter("newsid"));
                        NewsDAO.deleteNews(newsid);
                        redirect=true;
                        response.sendRedirect(response.encodeRedirectURL("../news/"));
                    }// view news item
                    else if (i == 4) {
                        int id = Integer.parseInt(request.getParameter("newsid"));
                        News news = (News) session.get(News.class, id);
                        request.setAttribute("news", news);
                        request.setAttribute("pageTitle", "View");
                    }//News item list
                    else if (i == 5) {
                        int course_id = Integer.parseInt(request.getParameter("courseid"));
                        System.out.println(course_id);
                        Course course = CourseDAO.getCourse(course_id);
                        List<News> newslist = NewsDAO.getNewsAllMax(course.getCourseid(),2);
                        request.setAttribute("newsitems", newslist);
                        request.setAttribute("amountnews", newslist.size());
                        request.setAttribute("pageTitle", "News items");
                        System.out.println("test");
                    }
                    // /user/profile
                   address = paths_jsp.get(i).toString();
                }
            } catch (Exception E) {
            }
        }

         request.setAttribute("path", path);
        if(!redirect){
           RequestDispatcher dispatcher = request.getRequestDispatcher(address);
            dispatcher.forward(request, response); 
        }
    }

//    public void doPost(HttpServletRequest request, HttpServletResponse response)
//            throws IOException, ServletException {
//        
//        ArrayList paths = getPaths();
//        ArrayList paths_jsp = getPathsJSP();
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        Transaction tx = session.beginTransaction();
//        News news = getNewsFromRequest(request);
//        session.saveOrUpdate(news);
//        tx.commit();
//        session.close();
//        response.sendRedirect("../news");
//    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        ArrayList paths = getPaths();
        ArrayList paths_jsp = getPathsJSP();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        String path = request.getServletPath();
        for (int i = 0; i < paths.size(); i++) {
            try {
                if (path.equals(paths.get(i).toString())
                        || path.substring(0, path.toString().length() - 1).equals(paths.get(i).toString())) {
                    address = paths_jsp.get(i).toString();
                    if (i == 1) {
                        System.out.println("New");
                        int course_id = Integer.parseInt(request.getParameter("newsglobal"));
                        Course course = CourseDAO.getCourse(course_id);
                        NewsDAO.insertNews(request, course);
                        response.sendRedirect(response.encodeRedirectURL("../news/"));

                    }
                    // ---------------- /news/update --------------------
                    if (i == 2) {
                        System.out.println("update");
                        int course_id = Integer.parseInt(request.getParameter("newsglobal"));
                        Course course = CourseDAO.getCourse(course_id);
                        NewsDAO.updateNews(request, course);
                        response.sendRedirect(response.encodeRedirectURL("../news/"));

                    }// ---------------- /news/Delete --------------------
                    else if(i == 3) {
                        System.out.println("Delete");
                        //int news_id = request.getParameter(path)
                        //NewsDAO.deleteNews(request);
                        response.sendRedirect(response.encodeRedirectURL("../news/"));
                    }//---------------- /course/enroll --------------------
                    else if(i == 5){
//                        int userid = UserController.UserAccountId;
//                        User user = UserDAO.getUser(userid);
//                        CourseDAO.updateCourse(request, user);
//                        System.out.println("Enrollment succesful!"); // Programming check
                    }
                }

            } catch (Exception E) {
            }
        }

    }

    
}
